package gold.digger;

import gold.utils.InputUtil;

import java.util.*;

/**
 * Created by fanzhenyu02 on 2021/12/10.
 * common problem solver template.
 */
public class LC1073 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int[] addNegabinary(int[] arr1, int[] arr2) {
            int i = arr1.length - 1, j = arr2.length - 1, len = Math.max(i + 1, j + 1) + 2;
            int c = 0, k = len - 1;
            int[] res = new int[len];

            while (i >= 0 || j >= 0 || c != 0) {
                if (i >= 0) c += arr1[i];
                if (j >= 0) c += arr2[j];
                res[k--] = Math.abs(c) % 2;
                if (c < 0) c = 1;
                else if (c > 1) c = -1;
                else c = 0;
                i--;
                j--;
            }

            //寻找第一位不为0的数
            int index = 0;
            while (index < len - 1 && res[index] == 0) {
                index++;
            }
            return Arrays.copyOfRange(res, index, len);
        }
    }


    public void run() {
        int[] arr = InputUtil.toIntegerArray("[1,2,3]");
        System.out.println(new Solution().toString());
    }

    public static void main(String[] args) throws Exception {
        LC1073 an = new LC1073();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
